package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.sys.dto.OrgDTO;
import com.bolt.admin.module.sys.entity.OrgEntity;
import com.bolt.admin.module.sys.service.OrgService;
import com.bolt.common.reflect.ObjectUtil;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


/**
 * Auto Generate Javadoc
 *
 * @author　米超
 * 日期:2018-02-27 14:20:36
 * 机构信息
 **/
@Controller
@RequestMapping("/admin/sys/org")
public class OrgController extends BaseController <OrgEntity, String> {

    @Autowired
    private OrgService orgService;

    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/sys/org/index", request, model.asMap());
    }

    @RequestMapping(value = "getOrgList")
    @ResponseBody
    public ResultMessage getOrgList(OrgDTO orgDTO) {
        return Returns.success(orgService.findOrg(orgDTO));
    }

    /**
     * 机构信息新增操作
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create() {
        return "/admin/sys/org/input-index";
    }

    /**
     * 机构信息编辑页
     *
     * @param
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(String id, Model model) {
        OrgDTO orgDTO = orgService.getOne(id);
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES,orgDTO);
        return "/admin/sys/org/input-index";
    }

    /**
     * 机构信息编辑操作
     *
     * @param orgDTO 表单数据
     * @return
     */
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    @LogAnnotation(module = "系统管理",description ="机构信息编辑操作")
    public ResultMessage saveOrUpdate( OrgDTO orgDTO) {
        orgService.saveOrUpdate(orgDTO);
        return  Returns.success("保存成功");
    }

    /**
     * 用户信息表删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    @LogAnnotation(module = "系统管理",description ="用户信息表删除操作")
    public ResultMessage doDelete(@RequestParam("id") String id) {
        if (ObjectUtil.isNotNull(id)) {
            orgService.deleteById(id);
            return  Returns.success("删除成功");
        } else {
            return Returns.error("所有选取记录删除操作失败");
        }
    }

    @RequestMapping(value = "select", method = RequestMethod.GET)
    public String select() {
        return "/admin/sys/org/org-select";
    }

    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public String selectAll() {
        return "/admin/sys/org/org-area-select";
    }

}